package org.jsbd.boss.action;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.DaylimitBean;
import org.jsbd.boss.domian.MonitorParam;
import org.jsbd.boss.domian.MonitorParamVO;
import org.jsbd.boss.service.IMonitorConfigService;
import org.jsbd.boss.service.IMonitorParamTimingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/monitor/param/timing")
public class MonitorParamTimingController {

	private static final Logger logger = LoggerFactory.getLogger(MonitorParamTimingController.class);

	@Autowired
	private IMonitorParamTimingService monitorParamTimingService;
	@Autowired
	private IMonitorConfigService monitorConfigService;

	/**
	 * 监控定时配置列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list() {
		return "monitorParam/list";
	}

	/**
	 * 监控定时配置数据
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/listJson", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> listJson(DataGridModel page, MonitorParamVO mvo) {
		return monitorParamTimingService.findDataWithPage(page, mvo);
	}

	/**
	 * 跳转新增页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String toAddPage() {
		return "monitorParam/add";
	}

	/**
	 * 新增
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public void add(HttpServletResponse response, MonitorParam monitorParam) {
		try {
			String param = monitorParam.getParam();
			boolean flag = monitorConfigService.hasParam(param);
			if (!flag) {
				ResponseUtils.responseParamNotExists(response);
			} else {
				monitorParam.setIsExecute(0);
				monitorParamTimingService.save(monitorParam);
				ResponseUtils.responseSuccess(response);
			}
		} catch (Exception e) {
			ResponseUtils.responseFailure(response);
		}
	}

	/**
	 * 跳转到编辑界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.GET)
	public String toUpdate(@PathVariable Integer id, Model model) {
		try {
			MonitorParam monitorParam = monitorParamTimingService.findMonitorParamById(id);
			;
			model.addAttribute("monitorParam", monitorParam);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return "monitorParam/update";
	}

	/**
	 * 修改
	 */
	@RequestMapping(value = "/{id}/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(@PathVariable Integer id, HttpServletResponse response, MonitorParam monitorParam) {
		try {
			String param = monitorParam.getParam();
			boolean flag = monitorConfigService.hasParam(param);
			if (!flag) {
				ResponseUtils.responseParamNotExists(response);
			} else {
				monitorParam.setId(id);
				monitorParam.setIsExecute(0);
				monitorParamTimingService.modifyMonitorParam(monitorParam);
				ResponseUtils.responseSuccess(response);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
	public void delete(@PathVariable Integer id, HttpServletResponse response) {
		try {
			monitorParamTimingService.delete(id);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}

}
